package com.terren.webdig.setting.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.terren.webdig.basic.impl.BasicServiceSupportImpl;
import com.terren.webdig.setting.entity.Function;
import com.terren.webdig.setting.entity.Role;
import com.terren.webdig.setting.entity.RoleFunction;
import com.terren.webdig.setting.service.IRoleService;

@Service("roleService")
public class RoleServiceImpl extends BasicServiceSupportImpl implements IRoleService {

	private final String SQL_MAPPER = "setting.roleMapper.";

	/**
	 * 获取角色信息
	 */
	@SuppressWarnings("unchecked")
	public List<Role> getRoleList(Role role) throws Exception {
		return (List<Role>) basicDao.findForList(SQL_MAPPER + "getRoleList", role);
	}

	/**
	 * 根据id获取角色信息
	 * 
	 * @param role
	 * @return
	 * @throws Exception
	 */
	public Role getRoleById(Role role) throws Exception {
		@SuppressWarnings("unchecked")
		List<Role> list = (List<Role>) basicDao.findForList(SQL_MAPPER + "getRoleById", role);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}

	}

	/**
	 * 根据Id查询角色
	 */
	public Role getRoleByIdAll(Role role) throws Exception {
		Role result = getRoleById(role);
		@SuppressWarnings("unchecked")
		List<Function> list = (List<Function>) basicDao.findForList(SQL_MAPPER + "getRoleFunctionByRole", role);
		String string = "";
		for (int i = 0; i < list.size(); i++) {
			if (i != list.size() - 1) {
				string += list.get(i).getFunctionId() + ",";
			} else {
				string += list.get(i).getFunctionId();
			}

		}
		result.setFunctionIds(string);
		result.setFunctionsList(list);

		return result;
	}

	/**
	 * 用户名是否存在
	 */
	@SuppressWarnings("unchecked")
	public List<Role> isRepeat(Role role) throws Exception {
		return (List<Role>) basicDao.findForList(SQL_MAPPER + "isRepeat", role);
	}

	/**
	 * 添加角色
	 */
	public String saveRole(Role role) throws Exception {
		basicDao.save(SQL_MAPPER + "saveRole", role);
		Integer cnt = role.getRoleId();
		String functionIds = role.getFunctionIds();
		// 角色跟功能的对应关系
		if (functionIds != null && !functionIds.trim().equals("") && !functionIds.trim().equals("null")) {
			List<RoleFunction> list = new ArrayList<RoleFunction>();
			String[] functionIdArray = functionIds.split(",");
			for (String functionId : functionIdArray) {
				RoleFunction roleFunction = new RoleFunction();
				roleFunction.setFunctionId(Integer.parseInt(functionId));
				roleFunction.setRoleId(role.getRoleId());
				list.add(roleFunction);
			}
			cnt += saveRoleFunction(list);
		}
		return String.valueOf(cnt);
	}

	/**
	 * 增加角色和功能的对应关系
	 * 
	 * @param list
	 * @return int
	 */
	public int saveRoleFunction(List<RoleFunction> list) throws Exception {
		return basicDao.saveList(SQL_MAPPER + "saveRoleFunction", list);
	}

	/**
	 * 更新用户
	 */
	public String updateRole(Role role) throws Exception {
		basicDao.update(SQL_MAPPER + "updateRole", role);// 更新角色
		Integer cnt = role.getRoleId();
		String functionIds = role.getFunctionIds();
		// 角色跟功能的对应关系
		if (functionIds != null && !functionIds.trim().equals("") && !functionIds.trim().equals("null")) {
			List<RoleFunction> newList = new ArrayList<RoleFunction>();
			String[] functionIdArray = functionIds.split(",");
			for (String functionId : functionIdArray) {
				RoleFunction roleFunction = new RoleFunction();
				roleFunction.setFunctionId(Integer.parseInt(functionId));
				roleFunction.setRoleId(role.getRoleId());
				newList.add(roleFunction);
			}
			RoleFunction roleFunction = new RoleFunction();
			roleFunction.setRoleId(role.getRoleId());
			// 原始的角色和功能对应关系
			deleteAllRoleFunction(roleFunction);
			cnt += saveRoleFunction(newList);
		}
		return String.valueOf(cnt);
	}

	public Object deleteAllRoleFunction(RoleFunction roleFunction) throws Exception {
		return basicDao.delete(SQL_MAPPER + "deleteAllRoleFunction", roleFunction);
	}

	/**
	 * 角色删除
	 */
	public Integer deleteRole(Role role) throws Exception {
		return (Integer) basicDao.save(SQL_MAPPER + "deleteRole", role);
	}

	/**
	 * 角色功能删除
	 */
	@Override
	public Integer deleteRoleFunction(RoleFunction roleFunction) throws Exception {
		return (Integer) basicDao.save(SQL_MAPPER + "deleteAllRoleFunction", roleFunction);
	}
}
